

var mongoose = require('mongoose')

var dbhost = '101.200.120.23'
var dbport = 27017
var dbname = 'wh2004my'
var dbuser = 'why'
var dbpass = 'loveyou'

// var conn_db_str = `mongodb://${dbhost}:${dbport}/${dbname}`;
var conn_db_str =  `mongodb://${dbuser}:${dbpass}@${dbhost}:${dbport}/${dbname}`

mongoose.connect(conn_db_str,{
    useNewUrlParser:true,
    useUnifiedTopology:true
},function(err){
    if(err){
        console.log('数据库连接失败');
    }else{
        console.log('数据库连接成功');
    }
})

const connection = mongoose.connection;

connection.on('connected',()=>{
    console.log('数据库已连接');
})


connection.on('error',()=>{
    console.log('数据库异常');
})


connection.on('disconnected',()=>{
    console.log('数据库断开连接');
})


module.exports = connection;